package com.security.authority.metadata;


import com.common.result.Result;
import com.common.util.CustomBeanUtil;
import com.security.domain.authority.CommonRole;
import com.security.service.AuthenticateService;
import lombok.RequiredArgsConstructor;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 远程从认知中心查询相关数据后返回
 * @author 大忽悠
 * @create 2022/10/10 17:52
 */
@RequiredArgsConstructor
public class RpcSecurityMetadataSource extends AbstractSecurityMetadataSource {
    private final AuthenticateService authenticateService;

    /**
     * 从数据库或者认证中心远程查询得到访问当前url需要具备的角色列表集合
     *
     * @param url
     * @return
     */
    @Override
    protected List<CommonRole> loadRoles(String url) {
        Result rolesListResult = authenticateService.getRolesOfRequestUrl(url);
        List<CommonRole> res = new ArrayList<>();
        List<Map> data = (List<Map>) rolesListResult.getData();
        //访问当前资源不需要具备任何角色
        if(data==null){
            return null;
        }
        data.forEach(map -> {
            res.add(CustomBeanUtil.mapToBean(map,new CommonRole()));
        });
        return res;
    }
}
